#include <unistd.h>

#include <kotf/kotf.hpp>

#include <thread>

using namespace kotf;

/*
  Note that you can't have more than one thread running for each class here
  because they would all share the same stream.
 */


struct HelloThread {
    Stream *stream;

    State world_state;

    HelloThread(Session& sess) :
        stream(&sess.getStream("HelloThread")),
        world_state(sess.getState("HelloThread::world"))
    { }
    

    void world()
    {
        Scope local(*stream, world_state);
        sleep(1);
    }

    void operator()()
    {
        stream->start();

        for (int i=0; i<3; ++i)
            world();

        stream->finish();
    }
    
};

struct ByeThread {
    Stream *stream;
    State world_state;

    ByeThread(Session& sess) :
        stream(&sess.getStream("ByeThread")),
        world_state(sess.getState("ByeThread::world"))
    { }

    void world()
    {
        Scope local(*stream, world_state);
        sleep(1);
    }

    void operator()()
    {
        stream->start();
        
        for (int i=0; i<3; ++i)
            world();

        stream->finish();
    }
};




int main()
{
    Session sess("hello-mt");
    
    std::thread t1 = std::thread(HelloThread(sess));
    std::thread t2 = std::thread(ByeThread(sess));

    t1.join();
    t2.join();
}




